//
//  BXPUserHomeHeaderView.swift
//  XerxesPP
//
//  Created by 向攀 on 2017/8/17.
//  Copyright © 2017年 Boos－XP. All rights reserved.
//

import UIKit


@objc protocol BXPUserHomeHeaderViewDelegate: NSObjectProtocol {
    @objc optional func userHomeHeaderViewDidClickedTopBackButton(_ headerView: BXPUserHomeHeaderView)
    @objc optional func userHomeHeaderViewTouchDownAction(_ headerView: BXPUserHomeHeaderView)
    @objc optional func userHomeHeaderViewTouchUpAction(_ headerView: BXPUserHomeHeaderView)
    
}

class BXPUserHomeHeaderView: UIView, UIGestureRecognizerDelegate {

    weak var delegate: BXPUserHomeHeaderViewDelegate?
    
    init(frame: CGRect, delegate: BXPUserHomeHeaderViewDelegate) {
        super.init(frame: frame)
        self.delegate = delegate
        
        setupUI()
        nickNameLabel.text = "Xerxes Panpan"
        profileLabel.text = "This is A Man 哈哈哈"
        
//        let panGes = UIPanGestureRecognizer(target: self, action: #selector(panGestureAction))
//        panGes.delegate = self
//        isUserInteractionEnabled = true
//        addGestureRecognizer(panGes)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() -> Void {
        backgroundColor = UIColor.white
        
        topBackImageView.image = UIImage(named: "profile_background")// profile_top_back
        topBackImageView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
//            make.height.equalTo(238)
//            make.height.equalTo(self.snp.height).offset(-200)
            make.bottom.equalToSuperview().offset(-200)
        }

        let coverView = UIView()
        coverView.backgroundColor = UIColor.white
        addSubview(coverView)
        coverView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(200)
        }


        avatorImageView.image = UIImage(named: "me_avator")
        avatorImageView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview().offset(-50)
            make.centerY.equalTo(self.snp.bottom).offset(-200)//(self.topBackImageView.snp.bottom)
            make.width.height.equalTo(150)
        }
//        setupNavLeftButton()
        setupAddFriendButton()
        setupNameInfo()
        
    }
    
    private func setupNavLeftButton() -> Void {
        let button = UIButton(frame: CGRect(x: 10, y: 20, width: 40, height: 40))
        button.contentEdgeInsets = UIEdgeInsets(top: 6, left: 0, bottom: 6, right: 20)
        button.setImage(UIImage(named: "back_white"), for: .normal)
        button.addTarget(self, action: #selector(navLeftBackButtonClicked), for: .touchUpInside)
        
        addSubview(button)
    }
    
    @objc private func navLeftBackButtonClicked() -> Void {
        if delegate != nil && (delegate?.responds(to: #selector(BXPUserHomeHeaderViewDelegate.userHomeHeaderViewDidClickedTopBackButton(_:))))! {
            delegate?.userHomeHeaderViewDidClickedTopBackButton!(self)
        }
    }
    
    
    private func setupAddFriendButton() -> Void {
        let addFriendButton = UIButton()
        addFriendButton.layer.cornerRadius = 25
        addFriendButton.layer.masksToBounds = true
        addFriendButton.backgroundColor = UIColor(intRed: 255, green: 238, blue: 88, alpha: 1.0)
        addFriendButton.setImage(UIImage(named: "add_friends_icon"), for: .normal)
        addFriendButton.addTarget(self, action: #selector(addFriendButtonClicked(_:)), for: .touchUpInside)
        addSubview(addFriendButton)
        addFriendButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(self.avatorImageView)
            make.left.equalTo(self.avatorImageView.snp.right).offset(50)
            make.width.height.equalTo(50)
        }
    }
    
    private func setupChattingButton() -> Void {
        let chatButton = UIButton()
        chatButton.layer.cornerRadius = 25
        chatButton.layer.masksToBounds = true
        chatButton.backgroundColor = UIColor(intRed: 255, green: 238, blue: 88, alpha: 1.0)
        chatButton.setImage(UIImage(named: "add_friends_icon"), for: .normal)
        chatButton.addTarget(self, action: #selector(chattingButtonClicked(_:)), for: .touchUpInside)
        addSubview(chatButton)
        chatButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(self.avatorImageView)
            make.left.equalTo(self.avatorImageView.snp.right).offset(50)
            make.width.height.equalTo(50)
        }
    }
    
    func setupNameInfo() -> Void {
        nickNameLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(self.avatorImageView.snp.bottom).offset(42)
            make.left.equalToSuperview().offset(50)
            make.right.equalToSuperview().offset(-50)
            
            //            make.width.greaterThanOrEqualTo(50)
            //            make.width.lessThanOrEqualTo(screenWidth - 100)
        }
        
        let topLine = UIView()
        topLine.backgroundColor = ColorSeparatorLine
        addSubview(topLine)
        topLine.snp.makeConstraints { (make) in
            make.left.right.equalTo(self.nickNameLabel)
            make.top.equalTo(self.nickNameLabel).offset(-12)
            make.height.equalTo(1)
        }
        
        let nameBottomLine = UIView()
        nameBottomLine.backgroundColor = ColorSeparatorLine
        addSubview(nameBottomLine)
        nameBottomLine.snp.makeConstraints { (make) in
            make.left.right.equalTo(self.nickNameLabel)
            make.top.equalTo(self.nickNameLabel.snp.bottom).offset(45)
            make.height.equalTo(1)
        }
        
        profileLabel.snp.makeConstraints { (make) in
            make.left.right.equalTo(self.nickNameLabel)
            make.top.equalTo(self.nickNameLabel.snp.bottom).offset(12)
        }
    }
    
    //MARK: - getter
    private lazy var topBackImageView: UIImageView = {
        let tempView = UIImageView()
        tempView.contentMode = .scaleAspectFill
//        tempView.layer.masksToBounds = true

        let tapGes = UITapGestureRecognizer(target: self, action: #selector(topBackImageViewTapAction))
        tempView.isUserInteractionEnabled = true
        tempView.addGestureRecognizer(tapGes)
        
        self.addSubview(tempView)
        return tempView
    }()
    
    private lazy var avatorImageView: UIImageView = {
        let tempView = UIImageView()
        tempView.layer.cornerRadius = 75
        tempView.layer.borderColor = UIColor.white.cgColor
        tempView.layer.borderWidth = 5
        tempView.layer.masksToBounds = true
        
        let tapGes = UITapGestureRecognizer(target: self, action: #selector(avatorImageViewTapAction))
        tempView.isUserInteractionEnabled = true
        tempView.addGestureRecognizer(tapGes)
        
        self.addSubview(tempView)
        return tempView
    }()
    
    lazy var nickNameLabel: UILabel = {
        let tempView = UILabel()
        tempView.font = UIFont.boldSystemFont(ofSize: 24)
        tempView.textColor = ColorText
        tempView.textAlignment = NSTextAlignment.center
        
        self.addSubview(tempView)
        return tempView
    }()
    
    lazy var profileLabel: UILabel = {
        let tempView = UILabel()
        tempView.font = PPYGlobalConstants.fontLevel2
        tempView.textColor = ColorTextLight
        tempView.textAlignment = NSTextAlignment.center
        
        self.addSubview(tempView)
        return tempView
    }()
    
    
    
    
    //MARK: - events
    @objc private func topBackImageViewTapAction() -> Void {
        touchUpAction()
    }
    
    @objc private func avatorImageViewTapAction() -> Void {
        touchUpAction()
    }
    
    @objc private func addFriendButtonClicked(_ button: UIButton) -> Void {
        touchUpAction()
    }
    
    @objc private func chattingButtonClicked(_ button: UIButton) -> Void {
        touchUpAction()
    }

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {

        if self.frame.contains(point) {
            touchDownAction()
        }

        

        return super.hitTest(point, with: event)
    }
    
    private func touchDownAction() -> Void {
        if delegate != nil && (delegate?.responds(to: #selector(BXPUserHomeHeaderViewDelegate.userHomeHeaderViewTouchDownAction(_:))))! {
            delegate?.userHomeHeaderViewTouchDownAction!(self)
        }
    }

    private func touchUpAction() -> Void {
        if delegate != nil && (delegate?.responds(to: #selector(BXPUserHomeHeaderViewDelegate.userHomeHeaderViewTouchUpAction(_:))))! {
            delegate?.userHomeHeaderViewTouchUpAction!(self)
        }
    }


    //MARK: - set scale
    func setViewScaleWithTableViewOffset(offsetY: CGFloat) -> Void {
        let offset = -offsetY


        let backViewHeight = topBackImageView.bounds.size.height
        let imageSize = topBackImageView.image?.size

        let imageH = (imageSize?.height)! * topBackImageView.bounds.size.width / (imageSize?.height)!

        let imageHeight = imageH > backViewHeight ? imageH : backViewHeight


        if imageHeight > (backViewHeight + offset) && offset < 300 {  //offset < 50 图片特别长时,不处理
            topBackImageView.snp.updateConstraints({ (make) in
                make.top.equalToSuperview().offset(offsetY)
            })
//            layoutIfNeeded()
        } else {
//            let scale = offset >= 200 ? offset / CGFloat(200) : (offset + backViewHeight) / imageHeight

            let scale = max(offset / CGFloat(300), (offset + backViewHeight) / imageHeight)

            if scale < 2.0 {
                topBackImageView.transform = CGAffineTransform(scaleX: scale, y: scale)
            }
        }
    }
}
